/*
 * LDIR
 * 
 * LDIR, a GPS cartography application built for Let's Do It Romania eco 
 * campaign.
 * Copyright (C) 2010 Genera Software <office@generasoftware.com>
 * 
 * http://generasoftware.com/
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
#ifndef __FADEPROVIDER_H
#define __FADEPROVIDER_H

// USER INCLUDES
#include <e32base.h>
#include "events.h"
#include "TimeUtils.h"

/**
* MTransitionProvider class declaration.
* Declares an interface for implementing transitions between two images.
*/
class MTransitionProvider
{
public:
    virtual ~MTransitionProvider () {}
    /**
    * Configures the transition.
    * @param aFirst The bitmap to start the transition from.
    * @param aSecond The bitmap to end the transition to.
    * @param aTransitionTime The time of the transition effect in milliseconds.
    */
    virtual void SetupTransition(CFbsBitmap* aFirst, CFbsBitmap* aSecond, TUint aTransitionTime) = 0;
    /**
    * Begins the transition.
    * The resulting intermediate images are provided via TransitionBitmap() function.
    */
    virtual void BeginTransition() = 0;
    /**
    * Stops the transition.
    * The transition is stopped immediately, with no other event raising.
    */
    virtual void StopTransition() = 0;
    /**
    * Retrieves the transition bitmap.
    */
    virtual CFbsBitmap* TransitionBitmap() const = 0;
    /**
    * The event raised when an intermediate image is completed.
    */
    TEvent0<void> StepCompleted;
    /**
    * The event raised when the transition completes.
    */
    TEvent0<void> TransitionCompleted;
};


/**
 * Responsible for fading images.
 */
class CFadeProvider : public CBase, public MTransitionProvider
{
public:
    /**
     * EPOC two phased constructor.
     */
    static CFadeProvider* NewL();
    /**
     * EPOC two phased constructor. Leaves a copy on the Cleanup Stack.
     */
    static CFadeProvider* NewLC();
    /**
     * Destructor.
     */
    ~CFadeProvider();
    /**
     * From MTransitionProvider, SetupTransition.
     */
    void SetupTransition(CFbsBitmap* aFirst, CFbsBitmap* aSecond, TUint aTransitionTime);
    /**
     * Sets the fading color.
     */
    void SetFadingColor(TRgb& aRgb);
    /**
     * Sets the fading type.
     * @param aFadeIn ETrue to fade in, EFalse to fade out.
     */
    void SetFadeDirection(TBool aFadeIn);
    /**
     * Retrieves the fading type.
     */
    TBool FadeDirection() const;
    /**
     * From MTransitionProvider, BeginTransition.
     */
    void BeginTransition();
    /**
     * From MTransitionProvider, StopTransition.
     */
    void StopTransition();
    /**
     * From MTransitionProvider, TransitionBitmap.
     */
    CFbsBitmap* TransitionBitmap() const;

protected:
    /**
     * Default constructor.
     */
    CFadeProvider();
    /**
     * EPOC second phase constructor.
     */
    void ConstructL();
    /**
     * Callback function for idle timer.
     */
    static TInt IdleCallback(TAny* aPtr);
    /**
     * Called from the static IdleCallback.
     */
    TInt TransitionStep();

    /**
     * Defines a function type that converts from a display mode to TRgb.
     */
    typedef TRgb (*TFromFunction) (TInt aValue);
    /**
     * Defines a method type that converts from TRgb to a display mode.
     */
    typedef TInt (TRgb::*TToFunction) () const;

    TUint GetTime();
private:
    /** @var iTransitionBitmap The transition bitmap.*/
    CFbsBitmap* iTransitionBitmap;
    /** @var iSourceBitmap The source bitmap.*/
    CFbsBitmap* iSourceBitmap;
    /** @var iSteps The number of steps to complete transition in.*/
    CFbsBitmapDevice* iTransitionBitmapDevice;
    CFbsBitGc* iTransitionBitmapGc;
    TBool iFadeIn;
    /** @var iTimer The timer to perform fading by.*/
    CIdle* iTimer;
    /** @var iTargetRgb Stores the fading color.*/
    TRgb iTargetRgb;
    /** @var iFromFunction Stores the conversion function from source display mode to TRgb.*/
    TFromFunction iFromFunction;
    /** @var iToFunction Stores the conversion method from TRgb to the destination display mode.*/
    TToFunction iToFunction;
    TRgb iFadeColor;
    TUint iTransitionTime;
    TTimeUtils iTimeUtils;
};

#endif //__FADEPROVIDER_H

